﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class UdfGridReportSettingDA : IUdfGridReportSettingDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string DATA_SQL = "@DATA_SQL";
        private string REMARK = "@REMARK";
        private string REPORT_ID = "@REPORT_ID";
        private string REPORT_NAME = "@REPORT_NAME";
        private string SQL_CHECK_UDF_GRID_REPORT_SETTING_ID_UNIQUE = " SELECT COUNT(1) FROM @_@UDF_GRID_REPORT_SETTING WHERE REPORT_ID = @REPORT_ID ";
        private string SQL_DELETE_UDF_GRID_REPORT_SETTING = " DELETE FROM @_@UDF_GRID_REPORT_SETTING WHERE REPORT_ID = @REPORT_ID ";
        private string SQL_INSERT_UDF_GRID_REPORT_SETTING = " INSERT INTO @_@UDF_GRID_REPORT_SETTING ( WH_ID, REPORT_ID, REPORT_NAME, REMARK, DATA_SQL, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @REPORT_ID, @REPORT_NAME, @REMARK, @DATA_SQL, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_UDF_GRID_REPORT_SETTING = " SELECT  ROWID, WH_ID, REPORT_ID, REPORT_NAME, REMARK, DATA_SQL, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@UDF_GRID_REPORT_SETTING ";
        private string SQL_UPDATE_UDF_GRID_REPORT_SETTING = " UPDATE @_@UDF_GRID_REPORT_SETTING SET WH_ID = @WH_ID, REPORT_NAME = @REPORT_NAME, REMARK = @REMARK, DATA_SQL = @DATA_SQL, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE REPORT_ID = @REPORT_ID  ";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckUdfGridReportSettingIDUnique(string udfGridReportSettingID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPORT_ID", udfGridReportSettingID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_UDF_GRID_REPORT_SETTING_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckUdfGridReportSettingIDUnique(DataBase dataBase, DbTransaction tran, string udfGridReportSettingID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPORT_ID", udfGridReportSettingID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_UDF_GRID_REPORT_SETTING_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteUdfGridReportSetting(List<string> udfGridReportSettingID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < udfGridReportSettingID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPORT_ID", udfGridReportSettingID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_UDF_GRID_REPORT_SETTING.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteUdfGridReportSetting(DataBase dataBase, DbTransaction tran, List<string> udfGridReportSettingID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < udfGridReportSettingID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPORT_ID", udfGridReportSettingID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_UDF_GRID_REPORT_SETTING.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllUdfGridReportSetting(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_UDF_GRID_REPORT_SETTING.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllUdfGridReportSetting(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_UDF_GRID_REPORT_SETTING.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public UdfGridReportSettingInfo GetUdfGridReportSettingByID(string udfgridreportsettingID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_UDF_GRID_REPORT_SETTING.Replace("@_@", whLoginID) + " WHERE REPORT_ID = @REPORT_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPORT_ID", udfgridreportsettingID) };
            UdfGridReportSettingInfo udfGridReportSettingInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    udfGridReportSettingInfo = new UdfGridReportSettingInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["REPORT_ID"].ToString(), reader["REPORT_NAME"].ToString(), reader["REMARK"].ToString(), reader["DATA_SQL"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return udfGridReportSettingInfo;
        }

        public UdfGridReportSettingInfo GetUdfGridReportSettingByID(DataBase dataBase, DbTransaction tran, string udfgridreportsettingID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_UDF_GRID_REPORT_SETTING.Replace("@_@", whLoginID) + " WHERE REPORT_ID = @REPORT_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPORT_ID", udfgridreportsettingID) };
            UdfGridReportSettingInfo udfGridReportSettingInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                udfGridReportSettingInfo = new UdfGridReportSettingInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["REPORT_ID"].ToString(), reader["REPORT_NAME"].ToString(), reader["REMARK"].ToString(), reader["DATA_SQL"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return udfGridReportSettingInfo;
        }

        public DataSet GetUdfGridReportSettingByQueryList(List<string> sqlWhere, UdfGridReportSettingQueryEntity udfgridreportsettingQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_UDF_GRID_REPORT_SETTING.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!udfgridreportsettingQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, udfgridreportsettingQuery.CurrentPage, udfgridreportsettingQuery.PageSize, udfgridreportsettingQuery.SortField, udfgridreportsettingQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetUdfGridReportSettingByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, UdfGridReportSettingQueryEntity udfgridreportsettingQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_UDF_GRID_REPORT_SETTING.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, udfgridreportsettingQuery.CurrentPage, udfgridreportsettingQuery.PageSize, udfgridreportsettingQuery.SortField, udfgridreportsettingQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertUdfGridReportSetting(UdfGridReportSettingInfo udfGridReportSettingInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                udfGridReportSettingInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_UdfGridReportSetting_Parameters(udfGridReportSettingInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_UDF_GRID_REPORT_SETTING.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertUdfGridReportSetting(DataBase dataBase, DbTransaction tran, UdfGridReportSettingInfo udfGridReportSettingInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                udfGridReportSettingInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_UdfGridReportSetting_Parameters(udfGridReportSettingInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_UDF_GRID_REPORT_SETTING.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_UdfGridReportSetting_Parameters(UdfGridReportSettingInfo udfGridReportSettingInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.REPORT_ID, SqlDbType.VarChar, 30), new SqlParameter(this.REPORT_NAME, SqlDbType.VarChar, 200), new SqlParameter(this.REMARK, SqlDbType.VarChar, 300), new SqlParameter(this.DATA_SQL, SqlDbType.VarChar, 0x1f40), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 30), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 30) };
            if (!string.IsNullOrEmpty(udfGridReportSettingInfo.WhID))
            {
                paramArray[0].Value = udfGridReportSettingInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = udfGridReportSettingInfo.ReportID;
            if (!string.IsNullOrEmpty(udfGridReportSettingInfo.ReportName))
            {
                paramArray[2].Value = udfGridReportSettingInfo.ReportName;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(udfGridReportSettingInfo.Remark))
            {
                paramArray[3].Value = udfGridReportSettingInfo.Remark;
            }
            else
            {
                paramArray[3].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(udfGridReportSettingInfo.DataSql))
            {
                paramArray[4].Value = udfGridReportSettingInfo.DataSql;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            paramArray[5].Value = udfGridReportSettingInfo.CreatedBy;
            paramArray[6].Value = udfGridReportSettingInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateUdfGridReportSetting(UdfGridReportSettingInfo udfGridReportSettingInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                udfGridReportSettingInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_UdfGridReportSetting_Parameters(udfGridReportSettingInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_UDF_GRID_REPORT_SETTING.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateUdfGridReportSetting(DataBase dataBase, DbTransaction tran, UdfGridReportSettingInfo udfGridReportSettingInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                udfGridReportSettingInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_UdfGridReportSetting_Parameters(udfGridReportSettingInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_UDF_GRID_REPORT_SETTING.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

